package com.portal.innovation.sysController;


import com.portal.innovation.utils.ExcelUtil;
import com.portal.innovation.utils.PageUtils;
import com.portal.innovation.common.R;
import com.portal.innovation.pojo.EnrollmentInfo;
import com.portal.innovation.pojo.ExportEnrollmentInfo;
import com.portal.innovation.service.EnrollmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("enrollment")
public class EnrollmentInfoController {

    @Autowired
    private EnrollmentInfoService enrollmentInfoService;

    @PostMapping("list")
    public R save(@RequestBody HashMap params) {
        PageUtils page = enrollmentInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    @PostMapping("save")
    public R save(@RequestBody EnrollmentInfo enrollmentInfo) {
        enrollmentInfo.setRegistrationTime(new Date());
        enrollmentInfo.setTagId(-1);
        enrollmentInfoService.save(enrollmentInfo);
        return R.ok();
    }

    @DeleteMapping("delete")
    public R save(@RequestBody Integer[] ids) {
        enrollmentInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping("update")
    public R update(@RequestBody EnrollmentInfo enrollmentInfo) {
        enrollmentInfoService.updateById(enrollmentInfo);
        return R.ok();
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        EnrollmentInfo enrollmentInfo = enrollmentInfoService.getById(id);
        return R.ok().put("enrollmentInfo", enrollmentInfo);
    }

    @GetMapping("/export")
    public void exportTeachers(HttpServletResponse response) {
        // 1. 查询所有教师数据
        List<ExportEnrollmentInfo> info = enrollmentInfoService.getAll();
        try {
            ExcelUtil.exportToExcel(response, info, ExportEnrollmentInfo.class, "报名信息表");
        } catch (IOException e) {
            throw new RuntimeException("导出Excel失败", e);
        }
    }
}
